/*
 * @Description:
 *
 * @Date: 2021-03-02 08:47:26
 */
const path = require('path')
const webpack = require('webpack')
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const { isProd, getLocalAddress, Proxy } = require('./build/utils')
const { UglifyJsPluginOptions, externals, splitChunksOptions } = require('./build/config.base')
function resolve(dir) {
	return path.join(__dirname, dir)
}
const localhost = getLocalAddress()
// const proxyURL = ['/commentTemplate/']
// const proxyPurchase = ['/purchase']
// 实例化 Proxy 类
const proxy = new Proxy()
// 添加代理数据，如需代理多个地址，继续调用实例的 addUrls 方法即可
proxy.addUrls(['/app-api', '/parttimeuserregister', '/captchaImage', '/companyregister', '/login'], 'localhost', 'http://localhost:48080')
// 配置
const publicPathConfig = {
	test: '/',
	prod: '/',
	dev: '/',
}
const outputDirPathConfig = {
	test: resolve('dist'),
	prod: resolve('dist'),
	dev: resolve('dist'),
}
module.exports = {
	// 打包上下文
	publicPath: process.env.buildenv ? publicPathConfig[process.env.buildenv] : './',
	// 打包输出的文件夹名
	outputDir: outputDirPathConfig[process.env.buildenv],
	// 资源目录
	assetsDir: 'static',
	// 生产环境不打包 map 文件
	productionSourceMap: false,
	// ellint检查
	lintOnSave: false,

	// 开发服务器配置
	devServer: {
		proxy: proxy.data,
		port: 8089,
	},

	// webpack配置 configureWebpack
	chainWebpack: (config) => {
		// 生产环境，或者开启本地CDN后，配置不打包的资源，需指定 cdn 加载
		config.plugin('html').tap((args) => {
			args[0].title = '中国共产党中国铁路广州局集团有限公司委员会党校'
			return args
		})
		if (isProd) {
			config.externals(externals)
		}
		// 生产环境
		if (isProd) {
			//代码切割
			config.optimization.splitChunks(splitChunksOptions)
		}
	},
	configureWebpack: (config) => {
		if (isProd) {
			// 压缩文件，去除调试信息
			config.plugins.push(new UglifyJsPlugin(UglifyJsPluginOptions))
		}
		config.plugins.push(
			new webpack.ProvidePlugin({
				$: 'jquery',
				jQuery: 'jquery',
				'windows.jQuery': 'jquery',
				'windows.$': 'jquery',
				Popper: ['popper.js', 'default'],
			})
		)
	},
	css: {
		loaderOptions: {
			sass: {
				prependData: `@import "@/assets/css/global.scss";`,
				// javascriptEnabled: true
			},
		},
		sourceMap: false,
	},
}
